<?php
require_once('kernel.inc');
require_once(LIBRARIES.'/image.class.inc');

if($AUTH->logedstatus === true && isset($AUTH->permission[$moduleid]))
{
   $NEWS = new kernel($moduleid);
   $NEWS->SetParams('user='.$AUTH->user['id'].';mode=d-;count=no;language='.$lang);

   if(isset($_REQUEST['dele']))
   {
      $NEWS->Delete($_REQUEST['dele']);
      SmartyPaginate::reset($_SYS['meccano']['pid']);
      SmartyPaginate::setLimit($MECCANO->system['items'], $_SYS['meccano']['pid']);
      SmartyPaginate::setPageLimit($MECCANO->system['pages'], $_SYS['meccano']['pid']);
   }

   $tree = $MECCANO->GetFlatList($moduleid);
   $SMARTY->assign('tree', $tree);
   $SMARTY->assign('newsimg', $MECCANO->system['newsimg']);

   switch ($event)
   {
      case 9:

      if($id !== false)
      {
         $vars = $NEWS->View($id);
         $SMARTY->assign_by_ref('vars', $vars);
      }

      case 10:

      if($submit != false)
      {
         $in['metawords']   = isset($_POST['metawords'])   ? prepare_str($_POST['metawords']) : '';
         $in['metadescr']   = isset($_POST['metadescr'])   ? prepare_str($_POST['metadescr']) : '';
         $in['title']       = isset($_POST['title'])       ? prepare_str($_POST['title'])     : false;
         $in['description'] = isset($_POST['description']) ? $_POST['description']            : '';
         $in['content']     = isset($_POST['content'])     ? $_POST['content']                : '';
         $in['topsite']     = isset($_POST['topsite'])     ? 1                                : 0;
         $in['hidden']      = isset($_POST['hidden'])      ? 1                                : 0;
         $in['short']       = isset($_POST['short'])       ? $_POST['short']                  : false;
         $in['source']      = isset($_POST['source'])      ? $_POST['source']                 : '';
         $in['tb_1']  = '';
         $in['tb_1w'] = '';
         $in['tb_1h'] = '';
         $in['tb_2']  = '';
         $in['tb_2w'] = '';
         $in['tb_2h'] = '';

         if(checkdate($_POST['Date_Month'], $_POST['Date_Day'], $_POST['Date_Year']))
         {
            $in['dat'] = $_POST['Date_Year'] . '-' . $_POST['Date_Month'] . '-' . $_POST['Date_Day'] . ' ' . date("H:i:s");
            unset($_POST['Date_Year'], $_POST['Date_Month'], $_POST['Date_Day']);
         } else
         {
            $in['dat'] = date("Y-m-d H:i:s");
         }

         // WORK WITH UPLOADED IMAGES
         $DB->sql = 'SELECT tb_1, tb_1w, tb_1h, tb_2, tb_2w, tb_2h FROM meccano_news WHERE id = ' . $id;
         $old_img = $DB->DB_FetchFlat();

         if(isset($_FILES['tb_1']['name']) && $_FILES['tb_1']['name'] != false)
         {
            $image = uploadfile($_FILES['tb_1']['name'], $MECCANO->system['newsimg'], $_FILES['tb_1']['tmp_name']);
            if($image != false)
            {
               $path = ROOT . $MECCANO->system['newsimg'] . '/' . $image;
               $IM = new Image();

               $destination = ROOT . $MECCANO->system['newsimg'] . '/t1_' . $image;
               $params = 'w='.$MECCANO->system['news_thumb_sw'].';h='.$MECCANO->system['news_thumb_sh'].';p='.$MECCANO->system['news_thumb_sp'].';s=1';
               $IM->SetParams($params);
               $img = $IM->Resize($path, $destination);
               if($img)
               {
                  $in['tb_1']  = 't1_' . $image;
                  $in['tb_1w'] = $img['width'];
                  $in['tb_1h'] = $img['height'];
                  if($old_img['tb_1'] != false) @unlink(ROOT.$MECCANO->system['newsimg'].$old_img['tb_1']);
               } else
               {
                  $in['tb_1']  = $old_img['tb_1'];
                  $in['tb_1w'] = $old_img['tb_1w'];
                  $in['tb_1h'] = $old_img['tb_1h'];
               }
               unlink($path);
            }
         }
         
      if(isset($_FILES['tb_2']['name']) && $_FILES['tb_2']['name'] != false)
         {
            $image = uploadfile($_FILES['tb_2']['name'], $MECCANO->system['newsimg'], $_FILES['tb_2']['tmp_name']);
            if($image != false)
            {
               $path = ROOT . $MECCANO->system['newsimg'] . '/' . $image;
               $IM = new Image();
               
               $destination = ROOT . $MECCANO->system['newsimg'] . '/t2_' . $image;
               $params = 'w='.$MECCANO->system['news_thumb_bw'].';h='.$MECCANO->system['news_thumb_bh'].';p='.$MECCANO->system['news_thumb_bp'];
               $IM->SetParams($params);
               $img = $IM->Resize($path, $destination);
               if($img)
               {
                  $in['tb_2']  = 't2_' . $image;
                  $in['tb_2w'] = $img['width'];
                  $in['tb_2h'] = $img['height'];
                  if($old_img['tb_2'] != false) @unlink(ROOT.$MECCANO->system['newsimg'].$old_img['tb_2']);
               } else
               {
                  $in['tb_2']  = $old_img['tb_2'];
                  $in['tb_2w'] = $old_img['tb_2w'];
                  $in['tb_2h'] = $old_img['tb_2h'];
               }
               unlink($path);
            }
         }

         if($category == false)    $error = 'category';
         if($in['title'] == false) $error = 'title';
         if($in['short'] == false) $error = 'anonce';

         if($error === false)
         {
            if($id !== false)
            {
               $DB->DB_QueryBuilder("UPDATE meccano_news SET 
                  topsite       = '%i', 
                  hidden        = '%i', 
                  metawords     = '%s',
                  metadescr     = '%s',
                  cat           = %i, 
                  dat           = '%s', 
                  title         = '%s', 
                  short_content = '%s', 
                  tb_1          = '%s',
                  tb_1w         = '%i',
                  tb_1h         = '%i',
                  tb_2          = '%s',
                  tb_2w         = '%i',
                  tb_2h         = '%i', 
                  description   = '%s', 
                  content       = '%s', 
                  source        = '%s', 
                  lang          = '%s' 
                  WHERE id      = %i;",
               array(
                  $in['topsite'], 
                  $in['hidden'], 
                  $in['metawords'],
                  $in['metadescr'],
                  $category, 
                  $in['dat'], 
                  $in['title'], 
                  $in['short'], 
                  $in['tb_1'],
                  $in['tb_1w'],
                  $in['tb_1h'],
                  $in['tb_2'],
                  $in['tb_2w'],
                  $in['tb_2h'], 
                  $in['description'], 
                  $in['content'], 
                  $in['source'], 
                  $lang, 
                  $id
               ));
               $make = $DB->DB_Query();
            } else
            {
               $DB->DB_QueryBuilder("INSERT INTO meccano_news (
                  topsite, hidden, 
                  metawords, metadescr,
                  cat, 
                  dat, 
                  user, 
                  title, 
                  short_content, 
                  tb_1, tb_1w, tb_1h, 
                  tb_2, tb_2w, tb_2h,
                  description, 
                  content, 
                  source, 
                  lang
                  ) VALUES ('%i', '%i', '%s', '%s', %i, '%s', %i, '%s', '%s', '%s', '%i', '%i', '%s', '%i', '%i', '%s', '%s', '%s', '%s');",
               array(
                  $in['topsite'],
                  $in['hidden'],
                  $category,
                  $in['dat'],
                  $AUTH->user['id'],
                  $in['title'],
                  $in['short'],
                  $in['tb_1'],
                  $in['tb_1w'],
                  $in['tb_1h'],
                  $in['tb_2'],
                  $in['tb_2w'],
                  $in['tb_2h'], 
                  $in['description'],
                  $in['content'],
                  $in['source'], 
                  $lang
               ));

               $make = $DB->DB_Query();
            }
         } else
         {
            $vars = $in;
            $SMARTY->assign('vars', $vars);
         }
      }

      $SMARTY->assign('error', $error);
      $SMARTY->assign('make', $make);

      $images = getDirList($_SERVER['DOCUMENT_ROOT'].$MECCANO->system['newsimg']);
      $SMARTY->assign('tumbn', $images['file']);

      $SMARTY->display('admin.editnews.tpl.html');
      break;

      //case 2:

      default:

      $vars = $NEWS->Get($category);
      if($vars)
      {
         SmartyPaginate::setTotal(count($vars), $_SYS['meccano']['pid']);

         $current_url = $_SERVER['PHP_SELF'];
         if($category != false) $current_url .= "?cat=" . $category;

         SmartyPaginate::setUrl($current_url, $_SYS['meccano']['pid']);
         $SMARTY->assign('vars', array_slice($vars, SmartyPaginate::getCurrentIndex($_SYS['meccano']['pid']), SmartyPaginate::getLimit($_SYS['meccano']['pid'])));
         SmartyPaginate::assign($SMARTY, 'paginate', $_SYS['meccano']['pid']);
      } else
      {
         $SMARTY->assign('vars', false);
      }

      $SMARTY->display('admin.news.tpl.html');
   }
} else
{
   echo $AUTH->frame_redirector();
}